package com.wicket.okrapp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wicket.okrapp.base.service.MOmsTspeGeneratedTaskEvaInfoService;
import com.wicket.okrapp.base.service.dto.req.BatchQueryTaskEvaInfoListByIdsReq;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.common.util.CommonFunctionHelper;
import com.wicket.okrapp.dal.mapper.mbg.OmsTspeGeneratedTaskEvaInfoMapper;
import com.wicket.okrapp.dal.po.mbg.OmsTspeGeneratedTaskEvaInfo;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsTspeGeneratedTaskEvaInfoServiceImpl
        extends ServiceImpl<OmsTspeGeneratedTaskEvaInfoMapper, OmsTspeGeneratedTaskEvaInfo>
        implements MOmsTspeGeneratedTaskEvaInfoService {

    @Autowired
    OmsTspeGeneratedTaskEvaInfoMapper omsTspeGeneratedTaskEvaInfoMapper;

    /**
     * 3-3-21批量查询任务评价信息列表ByIds[2570]
     */
    @Trace(operationName = "3-3-21批量查询任务评价信息列表ByIds")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTspeGeneratedTaskEvaInfo> batchQueryTaskEvaInfoListByIds(BatchQueryTaskEvaInfoListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsTspeGeneratedTaskEvaInfo> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getTaskEvaInfoList() == null || reqDto.getTaskEvaInfoList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("generated_task_id", reqDto.getTaskEvaInfoList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTspeGeneratedTaskEvaInfo.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTspeGeneratedTaskEvaInfo.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTspeGeneratedTaskEvaInfo.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTspeGeneratedTaskEvaInfo>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-21新增生成任务评价信息
     */
    @Trace(operationName = "3-3-21新增生成任务评价信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCreateTaskEvaInfo(OmsTspeGeneratedTaskEvaInfo omsTspeGeneratedTaskEvaInfo) {
        omsTspeGeneratedTaskEvaInfo.setTaskEvaInfoId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTspeGeneratedTaskEvaInfo.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTspeGeneratedTaskEvaInfo.setIsValid("FALSE");
        if (super.save(omsTspeGeneratedTaskEvaInfo)) {
            return omsTspeGeneratedTaskEvaInfo.getTaskEvaInfoId();
        } else {
            return "-1";
        }

    }
}
